/**
 *
 * @description:
 * @author:junyong.hong
 * @createTime:2017/12/27
 * @version:1.0.0.0
 * @history:
 *    1、
 *    2、
 *
 */

define([
    'app'
], function (app) {
    'use strict';
    app.directive('sideMenuTreeDirective', [function () {
        return {
            restrict: "EA",
            scope: {
                menuData: '='
            },
            templateUrl: 'js/directive/sideMenuTreeDirective/sideMenuTree.html',
            controller: function ($scope, $state, $rootScope) {
                /**
                 * 监听目录数据是否有变化
                 */
                var data = $scope.menuData;//目录原始数据
                $scope.$watch(function () {
                    return $scope.menuData;
                },function () {
                    data = $scope.menuData;
                    $rootScope.menuData = data;//把原始数据放到rootScope上

                    forData();
                });

                /**
                 * 遍历数据，判断是否可以展开
                 */
                function forData() {
                    angular.forEach(data, function (item, index) {
                        if (angular.isDefined(item.children)) {
                            item.isChildren = true;
                        } else {
                            item.isChildren = false;
                        }

                        item.state = false;//不展开
                    });

                    /**
                     * 对树形数据添加坐标
                     */
                    angular.forEach(data, function (value, key) {
                        var obj = {x: '', y: 0};
                        obj.x = key;
                        value.position = obj;
                        angular.forEach(value.children, function (data, index) {
                            var object = {x: '', y: ''};
                            object.x = key;
                            object.y = index + 1;
                            data.position = object;
                        })
                    });


                    $scope.resultData = data;//放在指令上传递
                }

                function init() {


                }

                init();
            },
            link: function (scope, elem, attr) {

            }
        }
    }])

        .directive('sideMenuTreeDirectiveBranch', [function () {
            return {
                restrict: "EA",
                scope: {
                    branchData: '=',
                },
                templateUrl: 'js/directive/sideMenuTreeDirective/sideMenuTreeBranch.html',
                controller: function ($scope, $state, $rootScope, $location, $stateParams) {
                    var str = '';
                    var branchData = $scope.branchData;
                    $rootScope.tabs = [{'name': '仪表盘', 'href': 'index.dashboard', 'position': {'x': '-1', 'y': '-1'}}];

                    /**
                     * 收起 or 展开
                     * @param data
                     */
                    $scope.close = function (data, e) {
                        e.stopPropagation();

                        if(data.state == false) {
                            data.state = true;
                        } else {
                            data.state = false;
                        }
                    };

                    /**
                     * 事件
                     * 结果
                     * 结果参数
                     * 哪里过来的
                     * 哪里过来的参数
                     */
                    // $rootScope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState, fromParams) {
                    //     console.log('$stateParams', $stateParams)
                    //     if(angular.isDefined(toParams.position)){
                    //         console.log('sdf',toParams.position.x);
                    //         console.log('sdf',toParams.position.y);
                    //     }
                    //
                    // });

                    /**
                     * 点击头部的tab，展开对应的目录
                     * $rootScope.menuData 目录的数据
                     */
                    $scope.$on('thisMenu', function (event, data) {
                        if(data.position.x == -1 && data.position.y == -1){//仪表盘
                            return;
                        }

                        if ($rootScope.menuData[data.position.x].children) {//有子集的情况下
                            var curMenu = $rootScope.menuData[data.position.x];

                            angular.forEach($rootScope.menuData, function (item) {// 默认全部收起
                                item.state = false;
                            });

                            curMenu.state = true;// 用户点击当前的tab，进行展开
                        }
                    });


                    /**
                     * 跳转到指定的路由
                     * @param node 当前点击的对象
                     * @param e
                     */
                    $scope.go = function (node, e) {
                        e.stopPropagation();

                        str = '';// 再次初始化为空值
                        if ($rootScope.tabs != []) {
                            for (var i = 0; i < $rootScope.tabs.length; i++) {
                                if (str.indexOf($rootScope.tabs[i].href) < 0) {
                                    str += $rootScope.tabs[i].href + ',';
                                }
                            }
                        }

                        if (str.indexOf(node.href) < 0) { // 如果str里未找到链接则添加数组
                            $rootScope.tabs.push({'name': node.name + '', 'href': node.href, position: node.position});
                        }

                        /**
                         * 发广播
                         * 父级（wrapper.controller.js）可以获取得到
                         *
                         * $rootScope.tabs 当前打开打tab数据
                         * node.name 当前激活的tab标题
                         */
                        $rootScope.$broadcast('tabData', [$rootScope.tabs, node.name]);

                        if (!node.children) {
                            $state.go(node.href);
                        }
                    };
                    $scope.state = $state;

                    $scope.resultData = branchData;


                    function init() {


                    }

                    init();

                }
            }
        }])

    // .factory('qsMenu',function ($rootScope,$timeout) {
    //
    //     function aa(data) {
    //
    //
    //
    //     }
    //
    //     return{
    //         'aa':aa
    //     }
    // })


});


